package activities;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.gms.maps.model.LatLng;
import com.searchparking.R;
import com.searchparking.BUS.DataSingleton;
import com.searchparking.BUS.ListviewAdapter;
import com.searchparking.BUS.ParseJsonData;
import com.searchparking.DTO.InfoObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.ImageView;
import android.widget.ListView;

import android.view.View.OnClickListener;

public class ListViewResult extends Activity {

	private ListView list;
	private ImageView img_home;
	private ArrayList<InfoObject> aParking;
	private Double latitude, longitude;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_view_result);
		Intent intent = new Intent();
		Bundle bundle = getIntent().getExtras();
		bundle.getDouble("latitude");
		
		latitude = bundle.getDouble("latitude");
		longitude = bundle.getDouble("longitude");
		
		int distance = intent.getIntExtra("distance", 10);
		aParking = new ArrayList<InfoObject>();
		LatLng point = new LatLng(latitude, longitude);
		JSONObject data = DataSingleton.data;
		ParseJsonData parseJon = new ParseJsonData();
		try {
			parseJon.parseData(data);
			aParking = parseJon.getListFromCurrent(distance, point);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ListviewAdapter listview = new ListviewAdapter(this, aParking);
		list = (ListView) findViewById(R.id.list_result_search);
		list.setAdapter(listview);
		//
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub

				Bundle bundle = new Bundle();
				bundle.putDouble("desLatitude",
						Double.parseDouble(aParking.get(arg2).getLati()));
				bundle.putDouble("desLongitude",
						Double.parseDouble(aParking.get(arg2).getLongi()));
				bundle.putDouble("latitude", latitude);
				bundle.putDouble("longitude", longitude);
				Intent direct_itennt = new Intent(getApplicationContext(),
						SearchDirection.class);
				direct_itennt.putExtra("Name", aParking.get(arg2).getName());
				direct_itennt.putExtra("Address", aParking.get(arg2).getAddr());

				direct_itennt.putExtras(bundle);
				Log.d("Vi tri",
						latitude
								+ " "
								+ longitude
								+ "  "
								+ Double.parseDouble(aParking.get(arg2)
										.getLati())
								+ ""
								+ Double.parseDouble(aParking.get(arg2)
										.getLongi()));
				 startActivity(direct_itennt);
				 finish();

			}
		});
		//
		img_home = (ImageView) findViewById(R.id.img_home);
		img_home.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent home_itennt = new Intent(getApplicationContext(),
						ResultSearchActivity.class);
				startActivity(home_itennt);
			}
		});
		// list view
	}
}
